//
//  ViewController.m
//  IOS-6day-1
//
//  Created by RIOS on 2018/4/14.
//  Copyright © 2018年 RIOS. All rights reserved.
//

#import "ViewController.h"
#import "MCars.h"

@interface ViewController () <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSArray *cars;
@property (weak, nonatomic) IBOutlet UITableView *vTableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.vTableView.dataSource = self;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSArray *)cars{
    if (_cars == nil) {
        _cars = [MCars buildArray];
     
    }
    return _cars;
}

- (BOOL)prefersStatusBarHidden{
    return true;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    MCars * model = self.cars[section];
    return model.title;
}

//-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
//    MCars * model = self.cars[section];
//    return model.describe;
//}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.cars.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   MCars * model = self.cars[section];
    return model.carNames.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MCars *model = self.cars[indexPath.section];
    
   static NSString *cellId = @"cell_1jfd";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        NSLog(@"创建单元格了:%ld",(long)indexPath.row);
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
  
    cell.textLabel.text = model.carNames[indexPath.row];
    return cell;
}

- (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
//    NSMutableArray *marray = [NSMutableArray new];
//    for (MCars *car in self.cars) {
//        [marray addObject:car.title];
//    }
    
    NSArray *array = [self.cars valueForKeyPath:@"title"];
    
    return array;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击了%ld",indexPath.row);
    
//    [tableView reloadData];
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}



@end
